﻿<%@ WebHandler Language="C#" Class="wxMiniProgramLogin" %>
using Models;
using System;
using System.Web;
using Newtonsoft.Json;
using System.Linq;
using System.Web.Configuration;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Net;


public class wxMiniProgramLogin : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        string openid = context.Request.Headers["X-ACCESS-USER"];//Header中的token

        var code = -1;
        var msg = "bang";
        var time = DateTime.Now;

        string xx = HttpContext.Current.Request.Form["Pid"];
        if (xx!="null")
        {

            var uid = Convert.ToInt32(xx);
            using (Entities db = new Entities())
            {

                if (uid > 0)
                {
                    var puser = db.User.FirstOrDefault(p => p.UID == uid);   //上级
                    var wxuser = db.User.FirstOrDefault(p => p.UOpenID == openid);

                    if (wxuser.UIndirectParentId == null && wxuser.UID != uid)
                    {
                        wxuser.UIndirectParentId = puser.UOpenID;
                        db.SaveChanges();
                    }

		          code = 1;
                          msg = "ok";
                }

            };

  

        }


        

        context.Response.Write(JsonConvert.SerializeObject(new { code, msg, time }));

        HttpContext.Current.Response.End();
        //context.Response.Write("Hello World");


    }


    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}